package faircat.android.exphone;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabWidget;

public class ExPhoneTabs extends TabActivity {
    private TabHost mTabHost;
    private Uri mDialUri;
    private String TAG = "ExPhoneTabs";
    public static final int TAB_INDEX_DIALER = 0;
    public static final int TAB_INDEX_CALL_LOG = 1;
    public static final int TAB_INDEX_CONTACTS = 2;
    public static final int TAB_INDEX_SMS = 3;
    public static final int TAB_INDEX_SETTINGS = 4;
    
    public static final String FAIRCAT_INTENT_ACTION_DIAL = "faircat.intent.action.DIAL";
    public static final String INTENT_TAB_CHANGE = "intent_tab_change";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
    	Log.d(TAG,"TabActivity----> onCreate");
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		mTabHost = getTabHost();		
		mTabHost.addTab(mTabHost.newTabSpec("dialer")
				.setIndicator(getResources().getString(R.string.dialpad), getResources().getDrawable(R.drawable.ic_tab_dialer))
				.setContent(new Intent(this, DialActivity.class)));
				
		mTabHost.addTab(mTabHost.newTabSpec("call_log")
				.setIndicator(getResources().getString(R.string.calllog), getResources().getDrawable(R.drawable.ic_tab_recent))
				.setContent(new Intent(this, CallLogActivity.class)));
		
		mTabHost.addTab(mTabHost.newTabSpec("contacts")
				.setIndicator(getResources().getString(R.string.contacts), getResources().getDrawable(R.drawable.ic_tab_contacts))
				.setContent(new Intent(this, Contacts.class)));
		
		mTabHost.addTab(mTabHost.newTabSpec("sms")
				.setIndicator(getResources().getString(R.string.sms), getResources().getDrawable(R.drawable.ic_tab_sms))
				.setContent(new Intent(this, SMS.class)));
		
		mTabHost.addTab(mTabHost.newTabSpec("settings")
				.setIndicator(getResources().getString(R.string.settings), getResources().getDrawable(R.drawable.ic_tab_setting))
				.setContent(new Intent(this, SettingsActivity.class)));
		/*
		TabWidget tabWidget = host.getTabWidget();
		for(int i = 0; i < tabWidget.getChildCount(); i++){
			View v = tabWidget.getChildAt(i); 
			v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_new_bg)); 
		}*/
			
	}
	
	@Override
	protected void onDestroy() {
		Log.d(TAG,"TabActivity----> onDestroy");
		// TODO Auto-generated method stub
		super.onDestroy();
		finish();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		Log.d(TAG,"TabActivity----> onNewIntent");
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		String action = intent.getAction();
    	System.out.println("TabActivity----> action:"+action);
		if(FAIRCAT_INTENT_ACTION_DIAL.equals(action)){
			int tabIndex = intent.getIntExtra(INTENT_TAB_CHANGE, -1);
		    if(tabIndex != -1 ){
		    	mTabHost.setCurrentTab(tabIndex);
		    }
		    setupDialUri(intent);
		}
	}
	
    public Uri getAndClearDialUri() {
        Uri dialUri = mDialUri;
        mDialUri = null;
        return dialUri;
    }

    private void setupDialUri(Intent intent) {
        mDialUri = intent.getData();
    }
}
